package com.ulife.item.module.screen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.ulife.category.dao.CategoryDao;
import com.ulife.category.dao.PropertyDao;
import com.ulife.category.dao.PropertyValueDao;
import com.ulife.category.dao.dataobject.CategoryDo;
import com.ulife.category.dao.dataobject.PropertyDo;
import com.ulife.category.dao.dataobject.PropertyValueDo;
import com.ulife.common.ErrorMessage;
import com.ulife.item.dao.ItemDao;
import com.ulife.item.dao.dataobject.IPVDo;
import com.ulife.item.dao.dataobject.ItemDo;
import com.ulife.item.module.vo.ItemVo;
import com.ulife.supplier.dao.SupplierDao;
import com.ulife.supplier.dao.dataobject.SupplierDo;

public class Itemlist {
	private static final Logger log = LoggerFactory.getLogger(Itemlist.class);

	@Autowired
	private SupplierDao supplierDao;

	@Autowired
	private ItemDao itemDao;

	@Autowired
	private PropertyValueDao propertyValueDao;

	@Autowired
	private PropertyDao propertyDao;

	@Autowired
	private CategoryDao categoryDao;

	@Autowired
	private HttpServletRequest request;

	public void execute(Context context, Navigator nav) {
		context.put("module", "item");
		String sId = request.getParameter("sId");
		if (StringUtils.isBlank(sId)) {
			context.put("msg", ErrorMessage.NOT_EXIST_SUPPLIER);
			nav.forwardTo("message");
			return;
		}
		try {
			SupplierDo sdo = supplierDao.querySupplierById(Long.valueOf(sId));
			if (sdo == null) {
				context.put("msg", ErrorMessage.NOT_EXIST_SUPPLIER);
				nav.forwardTo("message");
				return;
			} else {
				List<ItemDo> itemList = itemDao.queryItemsBySupplierId(sdo.getsId());
				List<ItemVo> itemVoList = new ArrayList<ItemVo>();
				for (Iterator iterator = itemList.iterator(); iterator.hasNext();) {
					ItemDo itemDo = (ItemDo) iterator.next();
					ItemVo itemVo = new ItemVo();
					BeanUtils.copyProperties(itemDo, itemVo);
					
					String cateName = itemDao.queryCategoryNameByItemId(itemDo.getItemId());
					itemVo.setCateName(cateName);

					List<IPVDo> ipvlist = itemDao.queryPVIdByItemId(itemDo.getItemId());

					Map<String, String> kv = new HashMap<String, String>();
					for (Iterator iterator2 = ipvlist.iterator(); iterator2.hasNext();) {
						IPVDo ipvDo = (IPVDo) iterator2.next();
						PropertyValueDo pv = propertyValueDao.queryPropertyValuesByPvId(ipvDo.getPvId());
						if (pv != null) {
							PropertyDo p = propertyDao.queryPropertyByPId(pv.getpId());
							if (p != null) {
								StringBuffer psb = new StringBuffer();
								psb.append(p.getpName());
								if (p.getpCode() != null) {
									psb.append("<" + p.getpCode() + ">");
								}
								StringBuffer pvsb = new StringBuffer();
								pvsb.append(pv.getPvName());
								if (pv.getPvCode() != null) {
									pvsb.append("<" + pv.getPvCode() + ">");
								}
								if (kv.get(psb.toString()) != null) {
									kv.put(psb.toString(), kv.get(psb.toString()) + "," + pvsb.toString());
								} else {
									kv.put(psb.toString(), pvsb.toString());
								}
							}
						}
					}
					StringBuffer pvvsb = new StringBuffer();
					Iterator iter = kv.entrySet().iterator(); 
					while (iter.hasNext()) { 
					    Map.Entry entry = (Map.Entry) iter.next(); 
					    Object key = entry.getKey(); 
					    Object val = entry.getValue(); 
					    pvvsb.append(key+":"+val+";");
					} 
					itemVo.setItemPVforListView(pvvsb.toString());
					itemVoList.add(itemVo);
				}

				context.put("items", itemVoList);
				context.put("supplier", sdo);

				List<CategoryDo> allcatelist = categoryDao.queryAllEnableCategory();
				context.put("allcatelist", allcatelist);
			}
		} catch (Exception e) {
			log.error("querySupplierById error", e);
		}

	}
}
